import danmaku from './danmaku';
import assSubtitle from './assSubtitle';
import { danmakuType } from './danmakuType';
import danmakuParser from './danmakuParser';

export class danmakuConverter {
    private convertToAss(danmakus: Array<danmaku>, videoWidth: number, videoHeight: number, fontName: string = "Microsoft YaHei", fontSize: number = 64,
        lineCount: number = 14, bottomMargin: number = 180, shift: number = 0.0): string {
        var header = "[Script Info]" + "ScriptType: v4.00+" + "Collisions: Normal" + `PlayResX: ${videoWidth}` + `PlayResY: ${videoHeight}` + `[V4+ Styles]` +

            "Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding" +
            "Style: AcplayDefault, {fontName}, {fontSize}, &H00FFFFFF, &H00FFFFFF, &H00000000, &H00000000, 0, 0, 0, 0, 100, 100, 0.00, 0.00, 1, 1, 0, 2, 20, 20, 20, 0"

        "[Events]" + "Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text";

        var dic1 = new Map<number, number>();
        var dic2 = new Map<number, number>();
        var asses: Array<assSubtitle> = [];
        danmakus.filter(d => d.type == danmakuType.normal || d.type == danmakuType.normal2 || d.type == danmakuType.normal3 || d.type == danmakuType.top || d.type == danmakuType.bottom || d.type == danmakuType.reverse)
            .forEach(d => asses.push(new assSubtitle(d, dic1, dic2, videoWidth, videoHeight, fontSize, lineCount, bottomMargin, shift)));
        return header + asses.join("\n");
    }

    convertToAssFromXml(danmakusXml: string, videoWidth: number, videoHeight: number, fontName: string = "Microsoft YaHei", fontSize: number = 64, lineCount: number = 14,
        bottomMargin: number = 180, shift: number = 0.0) {
        var danmakus = danmakuParser.loadXmlFromString(danmakusXml);
        return this.convertToAss(danmakus, videoWidth, videoHeight, fontName, fontSize, lineCount, bottomMargin, shift);
    }

    convertToAssFromFile(filePath: string, videoWidth: number, videoHeight: number, fontName: string = "Microsoft YaHei", fontSize: number = 64, lineCount: number = 14,
        bottomMargin: number = 180, shift: number = 0.0) {
        var danmakus = danmakuParser.loadXmlFromFile(filePath);
        return this.convertToAss(danmakus, videoWidth, videoHeight, fontName, fontSize, lineCount, bottomMargin, shift);
    }
}
export default new danmakuConverter();